<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\Controller;
class Attribute extends Common
{
    public function _initialize(){
        parent::_initialize();
    }
    //属性列表默认  擅长项
    public function index(){
        if(request()->isPost()) {
            //处理筛选条件
            $type = input('type')?input('type'):1;
            $where['type'] = array('eq',$type);


            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list = Db::name('attribute')
                ->where($where)
                ->order('sort')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['addtime'] = date('Y-m-d H:s',$v['addtime']);
                $list['data'][$k]['type'] = config('sx_type')[$v['type']]['name'];
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        $this->assign('sx_type',config('sx_type'));
        return $this->fetch();
    }

    //添加
    public function add(){
        if(request()->isPost()) {
            //构建数组
            $data = input('post.');
            $data['addtime'] = time();
            $typeId = explode(':',$data['type']);
            $data['type'] =$typeId[1];
            db('attribute')->insert($data);
            $result = [
                'code' =>1,
                'msg' => config('sx_type')[$typeId[1]]['name'].'属性添加成功',
                'url' => url('index')
            ];
            return $result;
        }else{
            $sx_type =config('sx_type');
            $this->assign('title',lang('add').'属性');
            $this->assign('info','null');
            $this->assign('sx_type',json_encode($sx_type,true));
            return $this->fetch('form');
        }
    }
    public function edit(){
        if(request()->isPost()) {
            $data = input('post.');
            $typeId = explode(':',$data['type']);
            $data['type'] =$typeId[1];
            db('attribute')->update($data);
            $result = [
                'code' =>1,
                'msg' => config('sx_type')[$typeId[1]]['name'].'属性添加成功',
                'url' => url('index')
            ];
            return $result;
        }else{
            $id=input('id');
            $sx_type =config('sx_type');
            $adInfo=db('attribute')->where(array('id'=>$id))->find();
            $this->assign('sx_type',json_encode($sx_type,true));
            $this->assign('info',json_encode($adInfo,true));
            $this->assign('id',$id);
            $this->assign('title',lang('edit').'属性');
            return $this->fetch('form');
        }
    }
    public function editState(){
        $id=input('post.id');
        $open=db('attribute')->where(array('id'=>$id))->value('is_show');//判断当前状态情况
        if($open==1){
            $data['is_show'] = 2;
            db('attribute')->where(array('id'=>$id))->setField($data);
            $data['status'] = 1;
        }else{
            $data['is_show'] = 1;
            db('attribute')->where(array('id'=>$id))->setField($data);
            $data['status'] = 1;
        }
        return $data;
    }
    public function adOrder(){
        $attr=db('attribute');
        $data = input('post.');
        if($attr->update($data)!==false){
            return $result = ['msg' => '操作成功！','url'=>url('index'), 'code' =>1];
        }else{
            return $result = ['code'=>0,'msg'=>'操作失败！'];
        }
    }
    public function del(){
        db('attribute')->where(array('id'=>input('id')))->delete();
        cache('adList', NULL);
        return ['code'=>1,'msg'=>'删除成功！'];
    }
    public function delall(){
        $map['ad_id'] =array('in',input('param.ids/a'));
        db('ad')->where($map)->delete();
        cache('adList', NULL);
        $result['msg'] = '删除成功！';
        $result['code'] = 1;
        $result['url'] = url('index');
        return $result;
    }

}